package com.chinasofti.info_manager.controller;



import com.chinasofti.info_manager.domain.Teacher;
import com.chinasofti.info_manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    TeacherService teacherService = new TeacherService();
    Scanner sc = new Scanner(System.in);

    public void start() {
        studentLoop:
        while (true){
            System.out.println("---------欢迎来到教师管理系统--------------");
            System.out.println("请您输入：1.添加教师2.删除教师3.修改教师4.查询教师5.退出");
            String choice = sc.next();

            switch (choice){
                case  "1":
                    System.out.println("添加教师");
                    addTeacher();
                    break;
                case  "2":
                    System.out.println("删除教师");
                    deleteTeacher();
                    break;
                case  "3":
                    System.out.println("修改教师");
                    updateTeacher();
                    break;
                case  "4":
                    System.out.println("查询教师");
                    findAllTeacher();
                    break;
                case  "5":
                    System.out.println("感谢使用教师管理系统，再见!");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
    }
    private void updateTeacher() {
        //输入合法的id
        String updateId = inputTeacherId();
        //手动录入更新对象
        Teacher newTea = inputTeacherInfo(updateId);
        //进行修改
        teacherService.updateTeacher(updateId,newTea);
    }

    private void deleteTeacher() {
        String delId = inputTeacherId();
        teacherService.deleteStudentById(delId);
        System.out.println("删除成功");
    }

    private String inputTeacherId() {
        String id;
        while(true){
            System.out.println("请输入id");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if(exists){
                System.out.println("您输入的id不存在，请重新输入！");
            }else {
                break;
            }
        }
        return id;
    }

    public void addTeacher() {
        String id;
        while(true){
            System.out.println("请输入老师的id：");
            id = sc.next();
            boolean flag = teacherService.isExists(id);
            if(flag){
                System.out.println("id可以使用！");
                break;
            }else{
                System.out.println("id被占用请重新输入!");
            }
        }

        //封装老师对象
        Teacher tea = inputTeacherInfo(id);
        //开始添加到数组
        boolean result = teacherService.addTeacher(tea);
        if(result){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }


    }

    private Teacher inputTeacherInfo(String id) {
        System.out.println("请输入老师姓名:");
        String name = sc.next();
        System.out.println("请输入老师年龄:");
        String age = sc.next();
        System.out.println("请输入老师生日:（格式：年年-月月-日日）");
        String birthday = sc.next();
        Teacher tea = new Teacher();
        tea.setId(id);
        tea.setAge(age);
        tea.setName(name);
        tea.setBirthday(birthday);
        return tea;
    }
    public void findAllTeacher() {
        Teacher[] teas = TeacherService.findAllTeacher();
        //判断stus是否为空？
        if(teas == null){
            System.out.println("查无信息，请添加后重试!");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i=0;i<teas.length;i++){
            Teacher tea = teas[i];
            if(tea != null){
                System.out.println(tea.getId()+"\t"+tea.getName()+"\t"+tea.getAge()+"\t"+tea.getBirthday());
            }
        }
    }
}
